package quote

class PersonController {

    def index() {}

    def table() {
        long personCount = Person.count()

        if (params?.lastPage) {
            params.offset = (personCount / ((params?.max ?: 10) as int)).intValue() * 10
        }

        List persons = Person.findAll("from Person", [max: (params?.max ?: 10) as int, offset: (params?.offset ?: 0) as int])
        render(template: 'table', model: [persons: persons, personCount: personCount])
    }

    def create() {
        def person = new Person(
                name: params.name,
                surname: params.surname,
                age: params.age as int,
                address: new Address(
                        city: params.city,
                        street: params.street,
                        house: params.house as int
                )
        )

        if (person.validate()) {
            person.save(validate: false)
        }

        redirect(action: 'table', params: [lastPage: true])
    }

    def person() {
        def person = Person.findById(params?.id as long)
        render(template: 'person', model: [person: person])
    }

}